package com.luck.easyfoodbatch.mvp.mine;

import android.content.Context;
import android.support.annotation.Nullable;

import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.callback.JsonCallback;
import com.luck.easyfoodbatch.mvp.model.Batches;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.model.HttpParams;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.mvp.mine
 * email：893855882@qq.com
 * data：16/11/4
 */
public class GetBatchesPresenter implements GetBatchesModel {
    private final String TAG = getClass().getSimpleName();
    private GetBatchesView batchesView;
    private Context mContext;

    public GetBatchesPresenter(GetBatchesView batchesView, Context mContext) {
        this.batchesView = batchesView;
        this.mContext = mContext;
    }

    @Override
    public void getBatches(String gid, String province, int type) {
        HttpParams params = new HttpParams();
        params.put("gid", gid);
        switch (type) {
            case 0:
                break;
            case 1:
                params.put("province", province);
                break;
        }
        OkHttpUtils.post(HttpUtil.GET_BATCHES)
                .tag(this)
                .cacheMode(CacheMode.DEFAULT)
                .cacheKey(TAG + gid)
                .params(params)
                .execute(new JsonCallback(mContext, Batches.class) {

                    @Override
                    public void onResponse(boolean isFromCache, Object data, Request request, @Nullable Response response) {
                        batchesView.batchesSuccess((Batches) data);
                        batchesView.hideLoading();
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                        batchesView.hideLoading();
                        batchesView.httpStatusError();
                    }
                });
    }
}
